﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using System.Windows.Navigation;
using Microsoft.Phone.UserData;
using LiveEduWizard.Theme;

namespace LiveEduWizard.Views
{
    public partial class SettingsPanel : PhoneApplicationPage
    {
        private NavigatedEventHandler navigationEventHandler;

        private Boolean selectedTabSettingLoaded;
        private Boolean backgroundColorSettingLoaded;
        private Boolean accentColorSettingLoaded;

        public SettingsPanel()
        {
            navigationEventHandler = new NavigatedEventHandler(NavigationService_Navigated);

            InitializeComponent();
            InitApplicationBar();

            //Live Edu Account
            string originalAccount = UserSettings.LiveEduAccount;
            foreach (var acc in new Appointments().Accounts)
            {
                AccountSetting.Items.Add(acc.Name);
            }
            if (AccountSetting.Items.Contains(originalAccount))
                AccountSetting.SelectedItem = originalAccount;

            // Selected tab after starting application
            String[] tabs = { "agenda", "current day" };
            SelectedTabSetting.ItemsSource = tabs;
            SelectedTabSetting.SelectedItem = UserSettings.SelectedTab;
            selectedTabSettingLoaded = true;

            // Simultaneous Scrolling
            TimetableGridSetting.IsChecked = UserSettings.SimultaneousTimetableGridScrolling;

            // Background
            String[] backgrounds = {"system", "light", "dark"};
            BackgroundSetting.ItemsSource = backgrounds;
            BackgroundSetting.SelectedItem = UserSettings.BackgroundColor;
            backgroundColorSettingLoaded = true;

            // Accent Colour
            AccentColourSetting.ItemsSource = ColorsSet.GetColorsSet();
            AccentColourSetting.SelectedIndex = UserSettings.AccentColorIndex;
            accentColorSettingLoaded = true;

        }

        private void InitApplicationBar()
        {
            ApplicationBar = new ApplicationBar();
            ApplicationBar.Mode = ApplicationBarMode.Default;
            ApplicationBar.IsVisible = true;
            ApplicationBar.IsMenuEnabled = true;
            InitApplicationBarButtons();
        }

        private void InitApplicationBarButtons()
        {
            ApplicationBarIconButton backButton = new ApplicationBarIconButton(ThemeHelper.BackButtonImage);
            backButton.Text = ThemeHelper.BackButtonText;
            backButton.Click += new EventHandler(backButton_Click);
            ApplicationBar.Buttons.Add(backButton);
        }

        void backButton_Click(object sender, EventArgs e)
        {            
            NavigationService.GoBack();
        }

        private void TimetableGridSetting_Checked(object sender, RoutedEventArgs e)
        {
            UserSettings.SimultaneousTimetableGridScrolling = true;
        }

        private void TimetableGridSetting_Unchecked(object sender, RoutedEventArgs e)
        {
            UserSettings.SimultaneousTimetableGridScrolling = false;
        }

        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            NavigationService.Navigated += navigationEventHandler;
        }

        private void AccountSetting_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (AccountSetting.SelectedItem is string)
                UserSettings.LiveEduAccount = (string)AccountSetting.SelectedItem;
        }

        void NavigationService_Navigated(object sender, NavigationEventArgs e)
        {
            if (e.Content != this)
            {
                //Unregister handler
                NavigationService.Navigated -= navigationEventHandler;

                //Set settings
                App.ViewModel.AllowSimultaneousScroll = UserSettings.SimultaneousTimetableGridScrolling;
                App.ViewModel.LiveEduAccount = UserSettings.LiveEduAccount;

                //Save settings
                UserSettings.Save();
            }
        }

        private void BackgroundSetting_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (backgroundColorSettingLoaded)
            {
                UserSettings.BackgroundColor = BackgroundSetting.SelectedItem.ToString();
            }

            if (String.Equals(BackgroundSetting.SelectedItem.ToString(), "system"))
            {
                AccentColourSetting.Visibility = Visibility.Collapsed;
                ThemeHelper.SetSystemTheme();
                ThemeHelper.UpdateMainBackground();
            }
            else
            {
                AccentColourSetting.Visibility = Visibility.Visible;
                if (backgroundColorSettingLoaded)
                {
                    ThemeHelper.SetTheme((String)BackgroundSetting.SelectedItem, ((WPColor)AccentColourSetting.SelectedItem).ColorTitle);
                    ThemeHelper.UpdateMainBackground();
                }
            }
        }


        private void AccentColourSetting_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (accentColorSettingLoaded)
            {
                UserSettings.AccentColorIndex = AccentColourSetting.SelectedIndex;
                ThemeHelper.SetTheme((String)BackgroundSetting.SelectedItem, ((WPColor)AccentColourSetting.SelectedItem).ColorTitle);
            }
        }

        private void SelectedTabSetting_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (selectedTabSettingLoaded && SelectedTabSetting.SelectedItem is String)
            {
                UserSettings.SelectedTab = (String)SelectedTabSetting.SelectedItem;
            }
        }


    }
}
